package com.example.demoexample.ui.base;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;

import com.example.demoexample.DemoApplication;
import com.example.demoexample.utils.NetWorkUtil;
import com.example.demoexample.utils.ToastUtil;


/**
 * Created by songdongliang on 2016/11/14.
 */

public class BaseActivity extends AppCompatActivity {

    protected ProgressDialog mProgressDialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (!checkNet()){
            ToastUtil.showMessageShort(getApplicationContext(),"请检查网络");
        }

    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    @Override
    protected void onPause() {
        super.onPause();

    }

    public void showProgressDialog(){
        if(mProgressDialog == null){
            mProgressDialog = ProgressDialog.show(this,null,"请稍等");
            mProgressDialog.setCancelable(false);
        }
        mProgressDialog.show();
    }

    public void dismissProgressDialog(){
        if(mProgressDialog != null && mProgressDialog.isShowing()){
            mProgressDialog.dismiss();
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK){
            if(mProgressDialog != null && mProgressDialog.isShowing()){
                mProgressDialog.dismiss();
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    public final boolean checkNet(){
        return NetWorkUtil.isNetworkAvailable(getApplicationContext());
    }

}
